﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class OrganizationViewEN : System.Web.UI.Page
{

    public List<OrgnizationChart> lstGridOrganizationChart
    {
        get
        {
            if (ViewState["lstGridOrganizationChart"] == null)
            {
                ViewState["lstGridOrganizationChart"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstGridOrganizationChart"].ToString()] == null)
            {
                Session[ViewState["lstGridOrganizationChart"].ToString()] = new List<OrgnizationChart>();

            }

            return (List<OrgnizationChart>)Session[ViewState["lstGridOrganizationChart"].ToString()];

        }
        set
        {
            if (ViewState["lstGridOrganizationChart"] == null)
            {
                ViewState["lstGridOrganizationChart"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstGridOrganizationChart"].ToString()] = value;
        }   
   
   

    }





    protected void Page_Load(object sender, EventArgs e)
    {



        Persons objPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objPersons);
       
        if (!Page.IsPostBack)
        {
            bindGrid("");
        }

    }



    /// <summary>
    /// this function will bind the grid 
    /// </summary>
    protected void bindGrid(string strIn)
    {
       
        List<OrgnizationChart> lstOrgnizationChart = search(strIn);

       

        lstGridOrganizationChart = lstOrgnizationChart;

        grdUsers.DataSource = lstOrgnizationChart;
        grdUsers.DataBind();
    }



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Oranization_chart_page))
            Response.Redirect("LoginEN.aspx");



        





    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bindGrid(txtSearch.Text);
    }


    /// <summary>
    /// this function will search in Company and return the values
    /// </summary>
    /// <param name="txtText">the text to search for</param>
    /// <returns>a list of items (Activity,Sector,Management,Division,Section) matched</returns>
    protected List<OrgnizationChart> search(string txtText)
    {

        OrgnizationChartDB objOrgnizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstRet = new List<OrgnizationChart>();
        if (txtText.Trim() == "")
        {
            List<OrgnizationChart> lstNew = objOrgnizationChartDB.Select(" top 100 ", " order by [NameEN] ");
            //lstNew = new List<OrgnizationChart>(from objFol in lstNew where objFol.StatusID != 2 select objFol);
            return lstNew;
        }

        string[] strValues = null;
        if (txtText.Trim()[0] == '\"' && txtText.Trim()[txtText.Trim().Length - 1] == '\"')
        {
            try
            {
                strValues = new string[] { txtText.Trim().Substring(1, txtText.Trim().Length - 2) };
                if (strValues[0] == "")
                    strValues = new string[0];
            }
            catch
            {
                strValues = new string[0];
            }
        }
        else
        {
            strValues = txtText.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }

        foreach (string strValue in strValues)
        {
            OrgnizationChart objOrgnizationChart = new OrgnizationChart();
            objOrgnizationChart.NameAR = objOrgnizationChart.NameEN = "%" + strValue + "%";

            List<OrgnizationChart> lstOrgnizationChart = objOrgnizationChartDB.SearchOrLike(objOrgnizationChart);
            //lstOrgnizationChart = new List<OrgnizationChart>(from objFol in lstOrgnizationChart where objFol.StatusID != 2 select objFol);

            foreach (OrgnizationChart objFol in lstOrgnizationChart)
            {
                if ((from objIn in lstRet where objIn.ID == objFol.ID select objIn).ToList().Count == 0)
                {
                    lstRet.Add(objFol);
                }
            }

        }


        return lstRet;



    }



    protected void grdUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        Persons objCurrentPersons = GlobalVars.getCurrentPerson();
        checkSecurityAndCustomizePage(objCurrentPersons);


        //security check

        if (!objCurrentPersons.lstAllRulesIDs.Contains((long)RulesEnum.Editing_Organiztion_chart_page))
        {
            lblError.Text = "Security error , you can view only";
            lblResult.Text = "";

            return;
        }


        OrgnizationChartDB objOrgnizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
        List<OrgnizationChart> lstOrgnizationChart = objOrgnizationChartDB.getOrgnizationChartByID(long.Parse(grdUsers.DataKeys[e.RowIndex]["ID"].ToString()));




        if (lstOrgnizationChart.Count == 0)
        {
            lblError.Text = "No items to delete";
            lblResult.Text = "";
            return;
        }





        //if (lstOrgnizationChart[0].lstChild.Count > 0 || lstOrgnizationChart[0].lstEmployees.Count > 0)
        //{
            lstOrgnizationChart[0].StatusID = 2;
            objOrgnizationChartDB.Update(lstOrgnizationChart[0]);
            lblResult.Text = "Deleted ok.";
            bindGrid(txtSearch.Text);
            return;
        //}


        //objOrgnizationChartDB.Delete(lstOrgnizationChart[0]);


        //lblResult.Text = "Deleted ok.";



        //bindGrid(txtSearch.Text);



    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrganizationEN.aspx");
    }
    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.DataSource = lstGridOrganizationChart;
        grdUsers.PageIndex = e.NewPageIndex;

        grdUsers.DataBind(); 
    }
}
